<template>
    <div class="loading display-flex" v-show="show">
        <div class="loading-animation">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
        <div class="load-body">
            <span>{{label}}</span>
        </div>
    </div>
</template>

<style scoped>
    .loading {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1020;
    }

    .loading-animation {
        position: relative;
    }

    .loading-animation > div {
        position: absolute;
        display: inline-block;
        width: 15px;
        height: 15px;
        background-color: white;
        border-radius: 100%;
        margin: 2px;
    }

    .loading-animation > div:nth-child(1) {
        top: 25px;
        left: 0;
        -webkit-animation: ball-spin-fade-loader 1s 0s infinite linear;
        animation: ball-spin-fade-loader 1s 0s infinite linear;
    }

    .loading-animation > div:nth-child(2) {
        top: 17.04545px;
        left: 17.04545px;
        -webkit-animation: ball-spin-fade-loader 1s 0.12s infinite linear;
        animation: ball-spin-fade-loader 1s 0.12s infinite linear;
    }

    .loading-animation > div:nth-child(3) {
        top: 0;
        left: 25px;
        -webkit-animation: ball-spin-fade-loader 1s 0.24s infinite linear;
        animation: ball-spin-fade-loader 1s 0.24s infinite linear;
    }

    .loading-animation > div:nth-child(4) {
        top: -17.04545px;
        left: 17.04545px;
        -webkit-animation: ball-spin-fade-loader 1s 0.36s infinite linear;
        animation: ball-spin-fade-loader 1s 0.36s infinite linear;
    }

    .loading-animation > div:nth-child(5) {
        top: -25px;
        left: 0;
        -webkit-animation: ball-spin-fade-loader 1s 0.48s infinite linear;
        animation: ball-spin-fade-loader 1s 0.48s infinite linear;
    }

    .loading-animation > div:nth-child(6) {
        top: -17.04545px;
        left: -17.04545px;
        -webkit-animation: ball-spin-fade-loader 1s 0.6s infinite linear;
        animation: ball-spin-fade-loader 1s 0.6s infinite linear;
    }

    .loading-animation > div:nth-child(7) {
        top: 0;
        left: -25px;
        -webkit-animation: ball-spin-fade-loader 1s 0.72s infinite linear;
        animation: ball-spin-fade-loader 1s 0.72s infinite linear;
    }

    .loading-animation > div:nth-child(8) {
        top: 17.04545px;
        left: -17.04545px;
        -webkit-animation: ball-spin-fade-loader 1s 0.84s infinite linear;
        animation: ball-spin-fade-loader 1s 0.84s infinite linear;
    }

    @keyframes ball-spin-fade-loader {
        50% {
            opacity: 0.3;
            -webkit-transform: scale(0.4);
            transform: scale(0.4);
        }

        100% {
            opacity: 1;
            -webkit-transform: scale(1);
            transform: scale(1);
        }
    }

    .load-body {
        width: 100px;
        height: 100px;
        border: 1px solid #e3e3e3;
        border-radius: 5px;
        margin: 40px 0 0 -40px;
        background-color: rgba(0, 0, 0, .5);
    }
    .load-body span {
        color: white;
        display: inline-block;
        width: 100%;
        text-align: center;
        margin-top: 75px;
    }
</style>

<script>
    export default {
        name: 'LoadAnimation',
        data () {
            return {
                show: this.load,
                time: null
            };
        },
        props: {
            load: {type: Boolean, default: false},
            timeout: {type: Number, default: 0},
            label: {type: String, default: '加载中'}
        },
        watch: {
            load (val) {
                this.show = val;
                this.initTimeOut();
            }
        },
        methods: {
            initTimeOut () {
                if (this.show !== true) {
                    return;
                }
                if (this.timeout > 0) {
                    if (this.time !== null) {
                        clearTimeout(this.timeout);
                    }
                    this.time = setTimeout(() => {
                        this.show = false;
                    }, this.timeout);
                }
            }
        },
        mounted () {
            this.initTimeOut();
        }
    };
</script>
